package com.gionee.wms.common;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.SecureRandom;

public class AesUtil {

    private final static String PASSWORD = "12345678";

    public static void main(String args[]) throws Exception {
        String content = "加密内容";
        String enStr = encrypt(content);
        //System.out.println(enStr);
        String result = decrypt("MUM5MDk3NDc1NDI0NUY0NEYwQjZDNkMxRDcyRTdFRTk0M0UyNEU0NTc0Mzk2MjRDQ0IyQzlCNEFCMkRDODJDQzkxNzVEQzgxNDlGNkVGODBFMEZBREE1M0I0RDNCNjhCMDBGRjJEMTc0OTA3NUY0MTA1NjlDQzgzOThDQjY4NDcxQzVBQTI1RDczNDlDQjc2RDEyRTA5NTU0Mzc0QTE3NzkyMDY5NjU0RUQ2NjM2NjVCNzc2MDE2NTZCQzY3NEE4RTM5Q0E2NDkxRTM4RTgzMjFBNzc5ODg5NEFERkJGRDdGQjFCMUFDMjc4MjI5QzA3M0ZFN0FGNkVCM0YyMEIyQTc4NzE0OEFCRTY5MkY1QzZBNzZDQTE4NzQzQTlBQTkwNjUyQjE1MzJDRUVCQjRDQTEzOTNBNkIwRUMwNTI2NzNGMkFERDZENTlGMTY0QzdGMEU0QUI3QUY0QUFBQkVFRkNDRTY5QjhDNjdEQUU2NjI2REJBODVFNTM1QUU0MDRDNkFEQ0Y4RDJFRDhCQjlDNUMwRjkyNDIwODk5OTA2QjY1RTRDMEMxQTA2NDQwRTQxODRCQjBCQTI3NjlFNEIyQkYyRDE5MzY2MEJDRjlCRjNBMThDMDZEMzNGQjdCQ0VDMjQ3NjMzMDhEOUMyMzNDMzRCRDQ1RkU2OTQ5RjRGNzU3QjFDNUU3RjQ4RDBBQ0Q1RkI1OERDRDRCREY3QzNDNTdFRDdFMEQ5N0NEQUU2NkVFNzRFMUIwRTE1MUQzOUVCMkQ3RDBGNUQzQ0RGNTAwMDMwQzMzQkM4QzE3MjA4QkYzRTZBRTM5QTY4QUJFQjMyNDYy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0RBQUNGMzY2RjE2RkU0MUEzQURGNTAxNkY5MDkyNjExQjBBRTk4OTM0MTNFNkZERTNCMTM5RTMyNTBCQjMwOEJBMzA3QURDQjIxOTY1OTk2MjA2ODcwMkFEQTBBQTZEODVEODE5ODA3NkQxQzBDMjYxOUQ1QkE4QTJFNDZGODAxQ0U5MzZFNDVDRDE0Rjc4RDE1RUQ1NEFEMDYxRjc1MjU3RDNCMjgxRTBDOTIxQzY4OEQxODU1Qzk0NTg2QzZCMzRDMzhGNjgyMzIyRkYxQTU2NzA3QzhBQjAwMDk5OTc0ODE1NjA2RTU2OEU2RDkxN0RFNzZBNkY0NjMyRDM3NTE0RDA0MTY0MDJGNzg0MzRCMDYyMzU0MzIxQzE2MTdGMDE2NjY1NDYwRTk4ODM4QTk5MzE4ODE3QTM4OUM2ODdGODVFMzQ5ODQyRTk0NjkwOEM5NURCNDgwOURBNTBBRkU5RTU2MzI3Q0NFODE0MzlBQkQyOTc1RDI1MzIwMERGRDcwM0M3RkY1MTMyMjM5MjlCNDk4QzExQkI5MTA3MUVCODRGRDk5OTY3NDk0RDA5REY0QjVFREI5MzU1RDFENEFCMkZEOEQyNEMxNkEwQUVBRDhGMDJEQUREQjVEREJDN0UxN0QxRjIwQUUxN0ZEMzM2NDdDOUYyQTk3ODYzMjFDODEyMDY1NjY2NkMzOTIzODVDMDYzMzU5NEEwRUU0MEE3N0EyMkI5RDYyREQ4ODkzOEIxMjFGM0JBNjAwMDQ1QkQ1RUQ2MUQ5QzBGRjlFMTI0ODhFMkJDRURGNDEzMjU0RDVGREE5NzMyRUFFRUM0Q0FCMzk3MTVDRDYxMjIzRjRGMjhDNDczQzAzNTIxMDA=");
        System.out.println(result);
    }

    /**
     * 加密
     * @param content  需要加密的内容
     * @param password 加密密码
     * @return
     */
    public static byte[] encrypt(String content, String password) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(password.getBytes());
        kgen.init(128, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
        byte[] byteContent = content.getBytes("utf-8");
        cipher.init(Cipher.ENCRYPT_MODE, key);// 初始化
        byte[] result = cipher.doFinal(byteContent);
        return result; // 加密
    }

    /**
     * 解密
     * @param content  待解密内容
     * @param password 解密密钥
     * @return
     */
    public static byte[] decrypt(byte[] content, String password) throws Exception {
        KeyGenerator kgen = KeyGenerator.getInstance("AES");
        SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
        random.setSeed(password.getBytes());
        kgen.init(128, random);
        SecretKey secretKey = kgen.generateKey();
        byte[] enCodeFormat = secretKey.getEncoded();
        SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
        Cipher cipher = Cipher.getInstance("AES");// 创建密码器
        cipher.init(Cipher.DECRYPT_MODE, key);// 初始化
        byte[] result = cipher.doFinal(content);
        return result; // 加密
    }

    /**
     * 将二进制转换成16进制
     * @param bytes
     * @return
     */
    public static String parseByte2HexStr(byte bytes[]) {
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < bytes.length; i++) {
            String hex = Integer.toHexString(bytes[i] & 0xFF);
            if (1 == hex.length()) {
                hex = '0' + hex;
            }
            sb.append(hex.toUpperCase());
        }
        return sb.toString();
    }

    /**
     * 将16进制转换为二进制
     * @param hexStr
     * @return
     */
    public static byte[] parseHexStr2Byte(String hexStr) {
        if (hexStr.length() < 1) return null;
        byte[] result = new byte[hexStr.length() / 2];
        for (int i = 0; i < hexStr.length() / 2; i++) {
            int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16);
            int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16);
            result[i] = (byte) (high * 16 + low);
        }
        return result;
    }

    /**
     * 加密
     * @param content
     * @return
     * @throws Exception
     */
    public static String encrypt(String content) throws Exception {
        byte[] enResult = encrypt(content, PASSWORD);
        String hexStr = parseByte2HexStr(enResult);
        String baseStr = Base64.getBase64(hexStr);
        return baseStr;
    }

    /**
     * 解密
     * @param content
     * @return
     * @throws Exception
     */
    public static String decrypt(String content) throws Exception {
        String enResult = Base64.getFromBase64(content);
        byte[] bt = parseHexStr2Byte(enResult);
        byte[] resultBt = decrypt(bt, PASSWORD);
        String result = new String(resultBt, "utf-8");
        return result;
    }
}
